package cn.apestar.modules.meibang.consultation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.modules.meibang.consultation.entity.CardResponse;
import cn.apestar.modules.meibang.consultation.entity.PackageResponse;
import cn.apestar.modules.meibang.consultation.entity.SearchApiRequest;
import cn.apestar.util.AuthUtil;

@Service
@Transactional(readOnly = true)
public class OrderService {

    private static String productList = "/java/meibang/product/listProduct";

    private static String treatList = "/java/meibang/product/listPacakge";

    private static String serviceList = "/java/meibang/product/listCard";

    @Autowired()
    RestTemplate restTemplateApestar;

    /**
     * 选择服务
     * @return void
     * @throws Exception
     * @author lvh 2016年6月23日 上午9:18:01</br> /** 疗程列表
     * @param request
     * @RequestMapping(value = "/listPacakge", method = RequestMethod.POST)
     */
    public Page findProductList(SearchApiRequest beautyConsultationOrderRecommend) {
        String url = Global.getConfig("USER_CLEAR_URL") + productList;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        AuthUtil.putAuth(headers);

        HttpEntity<SearchApiRequest> formEntity = new HttpEntity<SearchApiRequest>(beautyConsultationOrderRecommend, headers);

        ResponseEntity<Page> resultOn = restTemplateApestar.exchange(url, HttpMethod.POST, formEntity, Page.class);

        return resultOn.getBody();
    }

    /**
     * 疗程列表
     * @return void
     * @throws Exception
     * @author lvh 2016年6月23日 上午9:18:01</br>
     */
    public PackageResponse findTreatList(SearchApiRequest beautyConsultationOrderRecommend) {
        String url = Global.getConfig("USER_CLEAR_URL") + treatList;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        AuthUtil.putAuth(headers);

        HttpEntity<SearchApiRequest> formEntity = new HttpEntity<SearchApiRequest>(beautyConsultationOrderRecommend, headers);

        ResponseEntity<PackageResponse> resultOn = restTemplateApestar.exchange(url, HttpMethod.POST, formEntity, PackageResponse.class);

        return resultOn.getBody();
    }

    /**
     * 服务列表
     * @return void
     * @throws Exception
     * @author lvh 2016年6月23日 上午9:18:01</br>
     */
    public CardResponse findServiceList(SearchApiRequest beautyConsultationOrderRecommend) {
        String url = Global.getConfig("USER_CLEAR_URL") + "/java/meibang/product/listCard";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        AuthUtil.putAuth(headers);

        HttpEntity<SearchApiRequest> formEntity = new HttpEntity<SearchApiRequest>(beautyConsultationOrderRecommend, headers);

        ResponseEntity<CardResponse> resultOn = restTemplateApestar.exchange(url, HttpMethod.POST, formEntity, CardResponse.class);

        return resultOn.getBody();
    }

}
